package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    SetmealDishService setmealDishService;




    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealName", key = "#setmealDTO.id")
    public Result save(@RequestBody SetmealDTO setmealDTO){

        log.info("新增套餐：{}",setmealDTO);

        setmealDishService.save(setmealDTO);

        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> querySetmealByPage(SetmealPageQueryDTO setmealDTO){

        log.info("菜品分页查询：{}",setmealDTO);

        PageResult pageResult = setmealDishService.queryStmealByPage(setmealDTO);

        return Result.success(pageResult);

    }


    @GetMapping("/{id}")
    @ApiOperation("套餐数据回显")
    public Result<SetmealVO> setmealgetById(@PathVariable Long id){

        log.info("套餐数据回显：{}",id);

        SetmealVO setmealVO = setmealDishService.setmealById(id);

        return Result.success(setmealVO);

    }


    @PutMapping
    @ApiOperation("修改套餐数据")
    @CacheEvict(cacheNames = "setmealName", key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO){

        log.info("修改套餐数据：{}",setmealDTO);

        setmealDishService.update(setmealDTO);

        return Result.success();

    }


    @DeleteMapping()
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealName", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){

        log.info("删除套餐：{}",ids);

        setmealDishService.deleteByIds(ids);

        return Result.success();

    }

    @PostMapping("/status/1")
    @ApiOperation("起售")
    @CacheEvict(cacheNames = "setmealName", allEntries = true)
    public Result setmealStart(@RequestParam Long id ){

        log.info("起售：{}",id);

        setmealDishService.startOrstop(id);

        return Result.success();

    }

    @PostMapping("/status/0")
    @ApiOperation("停售")
    @CacheEvict(cacheNames = "setmealName", allEntries = true)
    public Result setmealStop(@RequestParam Long id ){

        log.info("起售：{}",id);

        setmealDishService.startOrstop(id);

        return Result.success();

    }



}
